package com.example.front.mapper;


import com.example.common.dto.Pagination;
import com.example.front.entity.BoxEventUser;
import com.example.front.entity.Maintenance;
import com.example.front.entity.MaintenanceUser;
import com.example.passport.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface MaintenanceUserMapper {

//    @Select("<script>" +
//            "SELECT * FROM `maintenance` " +
//            "where `status`!=" + User.STATUS_DELETE +
//            " ORDER BY `id` LIMIT #{pagination.offset}, #{pagination.limit}" +
//            "</script>")
//    List<Maintenance> findAll(@Param("maintenance") Maintenance maintenance, @Param("pagination") Pagination pagination);
//
//    @Select("<script>" +
//            "SELECT count(*) FROM `maintenance` " +
//           "where `status`!=" + User.STATUS_DELETE +
//            "</script>")
//    long count(@Param("maintenance") Maintenance maintenance);



    @Select("<script>" +
            "SELECT * FROM `maintenance_user` " +
            "<where>" +
            "<if test='maintenance.id!=null'> AND `m_id`=#{maintenance.id}</if>" +
            "</where>" +
            " AND `status`!=" + User.STATUS_DELETE +
            " AND `tenant_id`=#{maintenance.tenantId}"+
            "</script>")
    List<MaintenanceUser> findAll(@Param("maintenance") Maintenance maintenance);

    @Select("<script>" +
            "SELECT event_id FROM `maintenance_user` " +
            "<where>" +
            "<if test='maintenance.id!=null'> AND `m_id`=#{maintenance.id}</if>" +
            "</where>" +
            " AND `status`!=" + User.STATUS_DELETE +
            " AND `tenant_id`=#{maintenance.tenantId}"+
            " group by event_id" +
            "</script>")
    List<Long> group(@Param("maintenance") Maintenance maintenance);


    @Delete("delete from maintenance_user where m_id=#{id} and tenant_id=#{tenantId}")
    int delete(@Param("id") Long id,@Param("tenantId") Long tenantId);


    @Insert("INSERT INTO `maintenance_user`" +
            " (`m_id`,`event_id`,`user_id`,`level_id`,`tenant_id`) VALUES" +
            " (#{mId}, #{eventId}, #{userId},#{levelId},#{tenantId})")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int create(MaintenanceUser maintenanceUser);

    @Select("<script>" +
            "SELECT * FROM `maintenance_user` " +
            "<where>" +
            "m_id = #{maintenanceUser.mId}" +
            "<if test='maintenanceUser.eventId!=null'> AND `event_id`=#{maintenanceUser.eventId} </if>" +
            "</where> "+
            " AND `tenant_id`=#{maintenanceUser.tenantId}"+
            " AND `status`!=" + User.STATUS_DELETE +
            "</script>")
    List<MaintenanceUser> selectList(@Param("maintenanceUser") MaintenanceUser maintenanceUser);


}
